package com.just521.xinshoutong.xml;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.xmlpull.v1.XmlSerializer;

import android.util.Xml;



public class CommonXmlMaker {
	public String makexml(HashMap<String,Object> nodes) throws IOException{
		String  xml_ret = null;
		StringWriter writer = new StringWriter();
	    XmlSerializer serializer=Xml.newSerializer();  
//	    try{
	    	serializer.setOutput(writer);  
	        serializer.startDocument("utf-8", true);  
	        serializer.startTag(null, "root");  
		    
			Iterator it = nodes.entrySet().iterator();
			while(it.hasNext()){
	        	Map.Entry entry = (Map.Entry)it.next();
	        	String key = (String)entry.getKey();
	        	
	        	if(key.equals(XmlNode.Node_list)){
	        		ArrayList<HashMap<String, String>> list = (ArrayList<HashMap<String, String>>)entry.getValue();
	        		for(HashMap<String, String> rec : list){
	        			serializer.startTag(null, XmlNode.Node_rec);  
	        			Iterator itRec = rec.entrySet().iterator();
	        			while(itRec.hasNext()){
	        	        	Map.Entry entryRec = (Map.Entry)itRec.next();
	        	        	String keyRec = (String)entryRec.getKey();
	        	        	Object val=entryRec.getValue();
	        	        	String value = (String)val;
	    	        		serializer.startTag(null, keyRec);  
	    	        		serializer.text(value);  
	    	        		serializer.endTag(null, keyRec);  
	        			}
	        			
		        		serializer.endTag(null, XmlNode.Node_rec);  
	        		}
	        	}else{
	        		
	        		String value = (String)entry.getValue();
	        		serializer.startTag(null, key);  
	        		serializer.text(value);  
	        		serializer.endTag(null, key);  
	        	}
			}
			serializer.endTag(null, "root");  
	        serializer.endDocument();  
	        
	        xml_ret = writer.toString();	
//	    }catch (IOException e) {
//			// TODO: handle exception
//		}
                
        return xml_ret;
	}
		
	
};

